package ArticleService

import (
    "database/sql"
    "fmt"
    "server/app"
    "server/entity"
    "server/helper"
    "strings"
)

var (
    table = "article"
)

// 根据主键查询单个文章
func FindById(id string) (*entity.Article, error) {
    item := new(entity.Article)
    cols := colSQL() + ",Contents"
    sqlStr := fmt.Sprintf("select %v from %s where id=?", cols, table)
    helper.PrintLogSQL(sqlStr, id)
    err := app.GetDatabase().Get(item, sqlStr, id)
    if err == sql.ErrNoRows { //未查到记录
        return nil, nil
    }
    return item, err
}

// 查询要闻列表
func FindYaoList(count int) []entity.Article {
    return findSubList(count, "IsYao=1")
}

// 查询幻灯片列表
func FindSlideList(count int) []entity.Article {
    return findSubList(count, "IsSlide=1")
}

// 查询子模块列表
func findSubList(count int, where string) []entity.Article {
    list := make([]entity.Article, 0)
    limit := app.Empty
    if count > 0 {
        limit = fmt.Sprintf("top %d", count)
    }
    sqlStr := fmt.Sprintf("select %s %s from %s where Status=1 and %s order by RowIndex desc", limit, colSQL(), table, where)
    helper.PrintLogSQL(sqlStr)
    err := app.GetDatabase().Select(&list, sqlStr)
    app.Panic(err)
    return list
}

//查询旅游景点列表,可以指定读取数据量,大于0表示读取前几条
func FindTourList(count int) []entity.Article {
    list := make([]entity.Article, 0)
    categoryId := "488e4cf69b9446458aec29fe3a9143ee"
    limit := app.Empty
    if count > 0 {
        limit = fmt.Sprintf("top %d", count)
    }
    sqlStr := fmt.Sprintf("select %s Id,Subject,isnull(PhotoUrl,'') PhotoUrl,CreateTime from %s where CategoryId='%s' and IsIndex=1 order by CreateTime desc", limit, table, categoryId)
    helper.PrintLogSQL(sqlStr)
    err := app.GetDatabase().Select(&list, sqlStr)
    app.Panic(err)
    return list
}

// 查询文章列表
func FindList(filter *entity.ArticleFilter) ([]entity.Article, error) {
    list := make([]entity.Article, 0)
    condition, args := filterSQL(filter)
    sqlStr := fmt.Sprintf("select %s from %s %s", colSQL(), table, condition)
    helper.PrintLogSQL(sqlStr, args...)
    err := app.GetDatabase().Select(&list, sqlStr, args...)
    return list, err
}

// 查询文章分页列表
func FindPageList(pageFilter *entity.ArticlePageFilter) (*app.PageListResult, error) {
    filter := new(entity.ArticleFilter)
    err := helper.StructFieldCopy(pageFilter, filter)
    app.Panic(err)

    condition, args := filterSQL(filter)
    orderby := helper.BuildOrderBySQL(app.SortParam{SortName: pageFilter.SortName, SortDir: pageFilter.SortDir})

    list := make([]entity.Article, 0)
    return helper.FindPageList(&list, pageFilter.PageIndex, pageFilter.PageSize, table, colSQL(), condition, orderby, args...)
}

//获取列SQL
func colSQL() string {
    return `Id,Subject,isnull(CategoryId,'') CategoryId,isnull(CategoryName,'') CategoryName,Status,CreateTime,ModifyTime,IsSlide,isnull(PhotoUrl,'') PhotoUrl,isnull(Author,'') Author,isnull(Intro,'') Intro,isnull(RowIndex,0) RowIndex,IsIndex,IsYao`
}

//获取过滤条件SQL
func filterSQL(filter *entity.ArticleFilter) (condition string, args []interface{}) {
    if filter == nil {
        return
    }
    if len(filter.CategoryId) > 0 { //分类ID
        condition += "and CategoryId=? "
        args = append(args, filter.CategoryId)
    }
    if filter.Status > -1 { //状态
        condition += "and Status=? "
        args = append(args, filter.Status)
    }
    if filter.IsSlide > -1 { //是否首页幻灯片
        condition += "and IsSlide=? "
        args = append(args, filter.IsSlide)
    }
    if filter.IsIndex > -1 { //是否首页新闻
        condition += "and IsIndex=? "
        args = append(args, filter.IsIndex)
    }
    if filter.IsYao > -1 { //是否公交要闻
        condition += "and IsYao=? "
        args = append(args, filter.IsYao)
    }
    if len(args) > 0 {
        condition = "where " + strings.TrimLeft(condition, "and")
    }
    return
}
